﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using ZhiAo.Entities;
using System.Collections.Generic;
using ZhiAo.CommonLibrary;
using ZhiAo.Management.Data;
using ZhiAo.Management;

public partial class ShuiWenJu_GoogleMap : BasePage
{
    #region Properties
    public GoogleObject GoogleMapObject
    {
        get;
        set;
    }
    #endregion

    public delegate void PushpinMovedHandler(string pID);
    public event PushpinMovedHandler PushpinMoved;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (hidEventName.Value == "PushpinMoved")
        {
            hidEventName.Value = "";
            OnPushpinMoved(hidEventValue.Value);
        }
        if (!IsPostBack)
        {
            LoadGoogleMapObject();
        }
        else
        {
            GoogleMapObject = (GoogleObject)Session["GOOGLE_MAP_OBJECT"];
        }
    }

    public void OnPushpinMoved(string pID)
    {
        if (PushpinMoved != null)
        {
            GoogleMapObject = (GoogleObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
            PushpinMoved(pID);
        }
    }

    private void LoadGoogleMapObject()
    {
        GoogleMapObject = new GoogleObject();
        GoogleMapObject.APIKey = ZhiAo.Management.Utility.GetGooleMapAPIKey();
        Session["GOOGLE_MAP_OBJECT"] = GoogleMapObject;
        GooglePoint GP = null;
        string innerHtml = "";
        string functionId = PageHelper.Request("functionId");
        if (string.IsNullOrEmpty(functionId))
        {
            //1.判断当前用户是否管理员，是的话读杭州,不是取第一个地级市的functionId
            if (UserInfo.CurrentUserInfo.User.ID != "fa4bbe25399a4027852d02d998be98d0")
            {
                functionId = UserQuery.GetFirstCityId(UserInfo.CurrentUserInfo.User.ID);
            }
            else
            {
                functionId = Utility.GetDefaultFunctionId(); 
            }
                       
        }
        //List<SqStbprpB> lstSqStbprpB = QueryData.GetSqstbprpBList(functionId);
        List<DataRow> stationRows = QueryData.GetChildrenStations(functionId);
        int a = 0;

        GoogleMapObject.ZoomLevel = GetZoomLevel(stationRows.Count);


        foreach (DataRow item in stationRows)
        {
            double x = double.Parse(item["lttd"].ToString());
            double y = double.Parse(item["Lgtd"].ToString());

            a++;
            if (a == 1)
            {
                //GoogleMapObject.ZoomLevel = 10;
                GoogleMapObject.CenterPoint = new GooglePoint("1", x, y);
                Session["currentSiteId"] = item["yr_stcd"].ToString();
            }
            GP = new GooglePoint();
            GP.ToolTip = item["Stnm"].ToString();
            GP.IconImage = "icons/joystick.png";
            GP.Latitude = x;
            GP.Longitude = y;
            
            innerHtml = "<iframe src=\"MsLine.aspx?siteId=" + item["yr_stcd"].ToString() + "\" scrolling=\"no\" frameborder=\"0\" style=\"width:500px;height:260px;\"></iframe>";
            GP.InfoHTML = "<div>" + innerHtml + "</div>";
            GoogleMapObject.Points.Add(GP);
        }
    }

    private int GetZoomLevel(int stationCount)
    {
        int maxZoomlevel = 12;
        int mixZoomlevel = 10;
        int result = mixZoomlevel;
        if (stationCount == 1)
        {
            result = maxZoomlevel;
        }
        return result;
    }
}
